/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   ConfigFrameworkHelper.cs
 * CreateData   :   2023/7/11 15:16:46
 * UnityVersion :   2021.3.20f1c1
 * Description  :   框架配置表获取助手
************************************************/

using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;

namespace Main
{
    public static class ConfigFrameworkHelper
    {
        private static ComponentIDE data;                      // 数据组件
        static AssetBundle configAB;
        private static Dictionary<string, MonoBehaviour> objs = new Dictionary<string, MonoBehaviour>();
        private static ComponentIDE GetData
        {
            get
            {
                if (data == null)
                {
                    Unload();
#if UNITY_EDITOR && AB_Local
                    GameObject configObj = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(PathConfig.ConfigFrameworkFilePath);
#else
                    configAB = AssetBundlesManager.Instance.LoadAB("configframework");
                    GameObject configObj = configAB.LoadAsset<GameObject>("ConfigFramework");
#endif
                    GameObject obj = GameObject.Instantiate(configObj);
                    objs.Add("ConfigFramework", obj.GetComponent<MonoBehaviour>());
                    obj.hideFlags = HideFlags.HideInHierarchy;
                    data = obj.GetComponent<ComponentIDE>();
                }
                return data;
            }
        }
        /// <summary>
        /// 获取组件配置表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static FrameworkConfig GetConfig()
        {
            return null;
        }
        /// <summary>
        /// 卸载配置及其所有实例对象
        /// </summary>
        public static void Unload()
        {
            configAB?.Unload(false);
            List<MonoBehaviour> monos = objs.Values.ToList();
            foreach (MonoBehaviour mon in monos)
            {
                if (mon)
                {
                    GameObject.DestroyImmediate(mon.gameObject);
                }
            }
            objs.Clear();
            data = null;
        }
        /// <summary>
        /// 获取生成的对象节点
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T Get<T>(string name) where T : MonoBehaviour
        {
            if (objs.ContainsKey(name) && objs[name] != null)
            {
                return objs[name] as T;
            }
            else
            {
                return Generate<T>(name);
            }
        }
        private static T Generate<T>(string name) where T : MonoBehaviour
        {
            FrameworkConfig config = GetConfig();
            // TODO  生成物体
            GameObject go = config[name].prefab;
            GameObject obj = GameObject.Instantiate(go);
            T t = obj.GetComponent<T>();
            objs[name] = t;
            return t;
        }
    }
}
